package com.travel.group.controller;

import com.travel.group.common.PageResult;
import com.travel.group.entity.News;
import com.travel.group.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @RequestMapping("/toPage")
    public String toPage(){
        return "news/list";
    }

    //单独查询所用的接口
    @RequestMapping("/getAllNewsList")
    @ResponseBody
    public PageResult<News> getAllNewsList(Integer page, Integer limit, @RequestParam("name") String name) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<News> allArticle = newsService.getAllNewsInfoPlus(page, limit, name);
        Integer count = allArticle.size();
        PageResult<News> res = new PageResult<>(allArticle, count);
        return res;
    }

    //分页查询所有的信息
    @RequestMapping("/getAllNews")
    @ResponseBody
    public PageResult<News> getAllNews(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<News> allArticle = newsService.getAllNewsInfo(page, limit);
        Integer count = newsService.countData();
        PageResult<News> res = new PageResult<>(allArticle, count);
        return res;
    }

    //根据 id 进行删除操作
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(newsService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    //审核信息
    @RequestMapping("/check/{id}")
    @ResponseBody
    public PageResult check(@PathVariable("id") Integer id){
        News news = newsService.getById(id);
        news.setStatus(1);
        if(newsService.updateById(news)){
            return new PageResult("审核成功",200);
        }
        return new PageResult("审核失败",400);
    }
}
